from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time, random
from MyAutomator import MyAutomation



# Appium 基本参数
PLATFORM = 'Android'
DEVICE_NAME = 'MI_4S'
DRIVER_SERVER = 'http://localhost:4723/wd/hub'
TIMEOUT = 800
NORESET = 'True'
FLICK_STRAT_X = 300
FLICK_START_Y = 300
FLICK_DISTANCE = 700





class DongFangTouTiao(MyAutomation):

    def __init__(self, APP_PACKAGE, APP_ACTIVITY):

        # 圆形浮标
        self.coin_float_ele = 'com.songheng.eastnews:id/dd'#返回键

        # read
        self.subject = '//*[@text="财经"]'
        self.list_item_ele = 'com.songheng.eastnews:id/at9'

        # video
        self.tryplay_ele = '//*[@text="视频"]'
        # 视频列表play键
        self.tryplay_btn_ele = 'com.songheng.eastnews:id/au7'


        # minivideo
        self.trysmplay_ele = '//*[@text="小视频"]'
        self.trysmplay_btn2_ele = 'com.songheng.eastnews:id/t3'

        # 全局
        # 推送新闻框的忽略按钮
        self.ignore_push_news = 'com.songheng.eastnews:id/ea'
        # 升级框关闭叉叉
        self.close_update = 'com.songheng.eastnews:id/pj'

        super().__init__(APP_PACKAGE, APP_ACTIVITY)


    # minivideo
    def smallvideo(self, interval):
        video = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, self.tryplay_ele)))
        if len(video) > 1:
            video = video[1]
        video.click()
        print('点击底部菜单:视频...')
        video = self.wait.until(EC.presence_of_element_located((By.XPATH, self.trysmplay_ele)))
        video.click()
        print('点击底部菜单:点击短视频...')
        try:
            tryplay = self.wait.until(EC.presence_of_element_located((By.ID, self.trysmplay_btn2_ele)))
            tryplay.click()
            print('打开短视频')
            start = time.time()
            count = 0
            while True:
                time.sleep(random.randint(6, 12))
                self.swipeUp()
                end = start + interval
                now = time.time()
                count += 1
                print('page:' + str(count))
                print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now)))
                if now >= end:
                    print('结束小视频循环')
                    break
        except:
            print('打开短视频失败')

    # vido
    def video(self, interval):
        video = self.wait.until(EC.presence_of_element_located((By.XPATH, self.tryplay_ele)))
        video.click()
        print('点击底部菜单:视频...')

        t = 0
        start = time.time()
        while True:
            try:
                content = self.driver.find_elements_by_id(self.tryplay_btn_ele)
                print('视频数量', len(content))
                for i in range(len(content)):
                    print('第{}次进入循环.. 点击第{}视频'.format(t, i + 1))
                    content[i].click()
                    try:
                        coin_float = self.wait.until(EC.presence_of_element_located((By.ID, self.coin_float_ele)))
                        time.sleep(random.randint(50, 100))
                        # 检查推送信息
                        # self.check_push()
                    except:
                        print('无coin_float，应退回列表')

                    # 返回
                    self.driver.press_keycode(4)
                    print('退出视频')
            except:
                print('没有可播放视频')
            t += 1
            now = time.time()
            print('page:' + str(t))
            print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now)))
            self.swipeUp()
            time.sleep(random.randint(5, 10))
            end = start + interval
            now = time.time()
            if now >= end:
                print('结束视频循环')
                break

    # read
    def read_article(self, interval):

        suject = self.wait.until(EC.presence_of_element_located((By.XPATH, self.subject)))
        suject.click()

        start = time.time()
        while True:
            self.read()
            # if random.randint(2, 6) > 4:
            #     suject = self.wait.until(EC.presence_of_element_located((By.XPATH, self.subject)))
            #     suject.click()
            #     time.sleep(random.randint(2, 3))
            #     self.swipeUp()
            # else:
            try:
                for i in range(2):
                    time.sleep(random.randint(2, 4))
                    print('列表向下滑动')
                    self.swipeUp()
            except:
                a=1;

            end = start + interval
            now = time.time()
            if now >= end:
                print('结束新闻循环')
                break
            else:
                print('未到结束时间')


    def read(self):
        try:
            articles = self.wait.until(EC.presence_of_all_elements_located((By.ID, self.list_item_ele)))
            print('新闻数量', len(articles))
            for article in articles:
                try:
                    time.sleep(2)
                    article.click()
                    print('进入新闻')
                    try:
                        # 新闻
                        # coin_float = self.wait.until(EC.presence_of_element_located((By.ID, self.coin_float_ele)))
                        # if coin_float == 'null':
                        #     print('此为广告，应直接退出')
                        # else:
                        for i in range(2):
                            self.swipeUp()
                            time.sleep(random.randint(3, 5))

                        # try:
                        #     more = self.wait.until(
                        #         EC.presence_of_element_located((By.XPATH, '//*[@text="点击阅读全文"]')))
                        #     more.click()
                        #     print('点击查看更多')
                        #     self.swipeUp()
                        # except:
                        #     aaa = 0

                        for i in range(random.randint(2, 3)):
                            time.sleep(random.randint(3, 5))
                            self.swipeUp()

                    except:
                        print('在详情页报错，应直接退出')

                    # 返回
                    self.driver.press_keycode(4)
                    print('退出详情,回到列表1')


                except:
                    print('退出详情,回到列表2')
                    # self.swipeUp()
                print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

                # 检查推送信息
                # self.check_push()
        except:
            a=1

    # 检查更新消息
    def check_update(self):
        try:
            closeUpdate = self.wait.until(EC.presence_of_element_located((By.ID, self.close_update)))
            closeUpdate.click()
        except:
            print("无升级信息")

    # 检查推送消息
    def check_push(self):
        try:
            ignorPushNews = self.wait.until(EC.presence_of_element_located((By.ID, self.ignore_push_news)))
            ignorPushNews.click()
            time.sleep(3)
        except:
            print("无推送新闻")

    # 入口函数
    def main(self):
        print('打开 {} App ...'.format('dftt'))
        # self.check_update()
        self.check_push()
        # func_list = ['smallvideo', 'read_article', 'video']  # 新闻阅读，小视频，视频三个方法随机
        func_list = ['read_article']
        for i in range(12):
            exp = 'self.{}({})'.format(random.choice(func_list), random.randint(9000, 10000))
            print(exp)
            eval(exp)
            print('App {} 今日任务完成！'.format('dftt'))
            # home键
            self.driver.press_keycode(3)
            self.driver.close()



#dftt
qutoutiao_appPackage = 'com.songheng.eastnews'
qutoutiao_appActivity = 'com.oa.eastfirst.activity.WelcomeActivity'


if __name__ == '__main__':
    qutoutiao = DongFangTouTiao(qutoutiao_appPackage, qutoutiao_appActivity)
    qutoutiao.main()